package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.repository.StrategyRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
//@Transactional  暂时先别管事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private IUserInfoService userInfoService;
    
    @Autowired
    private StrategyCommentRepository repository;
    
    @Autowired
    private MongoTemplate template;
    
    
    
    @Override
    public StrategyComment get(String id) {
        return null;
    }
    
    //条件查询
    //page对象里面包括: 总页数 每页显示条数......
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        /*创建一个空的条件*/
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        /*工具类传进条件后 进行数据的排序与分页*/
        Page<StrategyComment> query1 = DBHelper.query(template, StrategyComment.class, query, qo.getPageable());
        //查询用户的信息
        return query1;
    }
    
    /*查询全部的内容*/
    public List<StrategyComment> list() {
        return repository.findAll();
    }
    
    
    /**
     * 删除方法
     *
     * @param id 删除的id
     */
    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }
    
    /**
     * 添加与保存的方法
     *
     * @param strategyComment
     */
    @Override
    public void save(StrategyComment strategyComment) {
        
        //防止在controller层的user注入将id添加了,这里将id设置为空
        strategyComment.setId(null);
        //创建的时间戳
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
        
    }
    
    /**
     * 点赞功能
     * @param cid   评论id
     * @param id    用户的id
     */
    @Override
    public void commentThumb(String cid, String id) {
        //大体思路就是 去这个评论的点赞集合中进行判断
            //获取这个评论的内容
        StrategyComment comment = repository.findById(cid).get();
        // 如果在集合中存在用户的id 那么就点赞状态设置为-1 同时将用户的id从集合中删除
        if (comment.getThumbuplist().contains(id)){
            comment.setThumbupnum(comment.getThumbupnum()-1);
            comment.getThumbuplist().remove(id);
        }else {//如果不存在 就点赞状态设置为+1  同时将用户id加进集合中
            comment.setThumbupnum(comment.getThumbupnum()+1);
            //添加
            comment.getThumbuplist().add(id);
        }
        repository.save(comment);
    }
    
    
}
